package com.wmh.baseservice.admin.controller;


import com.alibaba.fastjson.JSONObject;
import com.wmh.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.merchant.pojo.MerchantVegetablePlot;
import com.wmh.baseservice.merchant.service.MerchantVegetablePlotService;
import com.wmh.baseservice.merchant.service.MerchantVegetableService;
import com.wmh.baseservice.user.pojo.UsInfo;
import com.wmh.baseservice.user.pojo.UsVegetableOrder;
import com.wmh.baseservice.user.service.UsInfoService;
import com.wmh.baseservice.user.service.UsVegetableOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "管理员端-摘种订单相关接口")
@RestController
@RequestMapping(value = "admin/adVegetable", produces = "text/plain;charset=utf-8")
public class AdVegetableOrderController {

    @Resource
    private UsVegetableOrderService usVegetableOrderService;
    @Resource
    private MerchantVegetablePlotService merchantVegetablePlotService;
    @Resource
    private UsInfoService usInfoService;

    @ApiOperation("列表")
    @PostMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String list(PageInfo pageInfo, String orderNum, Integer type, Integer status, Long usId) {
        pageInfo.setTimeScreen("create_time");
        pageInfo.setDefaultSort("create_time", SortWay.DESC);
        //执行分页辅助工具
        PageResult<UsVegetableOrder> pageResult = new PageUtil<UsVegetableOrder>(pageInfo).startPage((page, queryWrapper) -> {
            if (CommUtil.checkNull(orderNum)) {
                queryWrapper.eq(UsVegetableOrder.ORDER_NUM, orderNum);
            }
            if (CommUtil.checkNull(type)) {
                queryWrapper.eq(UsVegetableOrder.TYPE, type);
            }
            if (CommUtil.checkNull(usId)) {
                queryWrapper.eq(UsVegetableOrder.US_ID, usId);
            }
            if (CommUtil.checkNull(status)) {
                queryWrapper.eq(UsVegetableOrder.STATUS, status);
            }
            usVegetableOrderService.page(page, queryWrapper);
        });
        return ReturnBody.success(pageResult);
    }

    @ApiOperation("详情")
    @PostMapping("detail")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    @ParameterVerify(notNull = "id")
    public String detail(Long id) {
        JSONObject object = new JSONObject(true);
        //菜地信息
        UsVegetableOrder usVegetableOrder = usVegetableOrderService.getByIdForUpdate(id);
        if (!CommUtil.checkNull(usVegetableOrder)) {
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        object.put("order", usVegetableOrder);
        object.put("plotId",usVegetableOrder.getPlotId());
        object.put("merchantId",usVegetableOrder.getMerchantId());
        UsInfo usInfo = usInfoService.getByIdForUpdate(usVegetableOrder.getUsId());
        if (!CommUtil.checkNull(usInfo)){
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        object.put("nick",usInfo.getNick());
        object.put("phone",usInfo.getPhone());
        object.put("avatar",usInfo.getAvatar());
        return ReturnBody.success(object);
    }
}
